//
//  ViewController.m
//  动态输入框
//
//  Created by yuanjiang on 2016/11/23.
//  Copyright © 2016年 YuanJiang. All rights reserved.
//

#import "ViewController.h"
#import <Masonry.h>
#define kMaxTextCount 600
@interface ViewController ()<UIScrollViewDelegate,UITextViewDelegate>

@property(nonatomic,strong) UIScrollView *scroll;
@property(nonatomic,strong) UITextView *textView;
@property(nonatomic,strong) UILabel *numofText;
@property(nonatomic,strong) UIButton *button;
@property(nonatomic,strong) UIView *containView;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupUI];
}
-(void)setupUI
{
    self.view.backgroundColor = [UIColor whiteColor];
    
    
    self.scroll = [[UIScrollView alloc] init];
    self.scroll.backgroundColor = [UIColor whiteColor];
    self.scroll.delegate=self;
    [self.view addSubview:_scroll];
    [self.scroll mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    
    self.containView = [[UIView alloc] init];
    self.containView.backgroundColor = [UIColor whiteColor];
    [self.scroll addSubview:self.containView];
    [self.containView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.scroll);
        make.width.equalTo(self.scroll);
        make.height.equalTo(self.scroll);
    }];
    
    
    self.textView = [[UITextView alloc] init];
    self.textView.text= @"您最多可以输入600个字";
    self.textView.layer.masksToBounds=YES;
    self.textView.delegate=self;
    self.textView.scrollEnabled=NO;
    self.textView.layer.borderColor = [UIColor orangeColor].CGColor;
    self.textView.layer.borderWidth =1;
    [self.containView addSubview:self.textView];
    [self.textView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.view.mas_left).offset(30);
        make.top.mas_equalTo(self.view.mas_top).offset(50);
        make.right.mas_equalTo(self.view.mas_right).offset(-30);
        make.height.mas_equalTo(100);
    }];
    
    self.numofText = [[UILabel alloc] init];
    self.numofText.text =[NSString stringWithFormat:@"0/%d  ",kMaxTextCount];
    self.numofText.textAlignment = NSTextAlignmentRight;
    [self.containView addSubview:self.numofText];
    [self.numofText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.textView.mas_right);
        make.top.mas_equalTo(self.textView.mas_bottom).offset(20);
        make.width.mas_equalTo(100);
        make.height.mas_equalTo(30);
    }];
    
    self.button = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.button setTitle:@"确定按钮" forState:UIControlStateNormal];
    [self.button setBackgroundColor:[UIColor orangeColor]];
    [self.button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.button addTarget:self action:@selector(clickAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.containView addSubview:self.button];
    [self.button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.textView.mas_bottom).offset(70);
        make.centerX.mas_equalTo(self.textView.mas_centerX);
        make.width.mas_equalTo(80);
        make.height.mas_equalTo(30);
    }];
    

}

-(void)clickAction:(UIButton *)sender
{
    NSLog(@"111");
}

-(void)textViewDidBeginEditing:(UITextView *)textView
{
    if ([textView.text isEqualToString:@"您最多可以输入600个字"]) {
        textView.text =@"";
        textView.textColor = [UIColor redColor];
    }
}


-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    
    self.numofText.text = [NSString stringWithFormat:@"%lu/%d",_textView.text.length,kMaxTextCount];
    CGRect orgRect = _textView.frame;
    CGSize size = [_textView sizeThatFits:CGSizeMake(_textView.frame.size.width, MAXFLOAT)];
    orgRect.size.height = size.height+5;
   
    if (orgRect.size.height>105) {
        [self.textView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.scroll.mas_left).offset(30);
            make.top.mas_equalTo(self.scroll.mas_top).offset(50);
            make.right.mas_equalTo(self.scroll.mas_right).offset(-30);
            make.height.mas_equalTo(orgRect.size.height);
        }];
    }
    // scrollerView 发挥作用的时候
    NSInteger height =  self.button.frame.origin.y+ self.button.frame.size.height;
    if (height> self.containView.frame.size.height) {
//        [self.containView mas_updateConstraints:^(MASConstraintMaker *make) {
//        make.edges.equalTo(self.scroll);
//        make.width.equalTo(self.scroll);
//        make.height.mas_equalTo(self.button.mas_bottom).offset(20);
//        }];
        [self.containView setFrame:CGRectMake(0, 0, self.view.frame.size.width, height)];
        
    }
    
    
    
    if (textView.text.length>600) {
        return NO;
    }else{
        return YES;
    }
    
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
